<template>
  <div>
    <div class="products-holder">
      <div v-for="product in products" :key="product.id" class="product-holder">
        <img class="product-img" :src="productPictureUrl + product.id">
        <!-- <div class="product-img"></div> -->
        <div class="product-desc">
          <div class="product-name">{{ product.name }}</div>
          <div class="product-amount">
            <span style="float:left;">月售{{ product.sellamount }}份</span>
            <span style="float:right;margin-right:10px;">库存{{ product.restamount }}份</span>
          </div>
          <div class="product-options">
            <span class="product-price">￥{{ product.price }}</span>
            <div class="product-add-to-cart-button">
              <div class="product-add-to-cart-button-decrease" @click="removeOneFromShoppingCart(product)">-</div>
              <div class="product-add-to-cart-button-num">{{ getProductAmount(product.id) }}</div>
              <div class="product-add-to-cart-button-increase" @click="addProductToShoppingCart(product)">+</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapGetters, mapActions } from 'vuex'
import { FileApi } from 'services/apiConfig.js'

export default {
  data() {
    return {
      selectedProject: null,
      createProjectFormVisible: false,
      createProjectForm: {
        name: '',
        description: ''
      },
      productPictureUrl: FileApi.product.getUrl
    }
  },
  computed: mapGetters({
    shoppingCart: 'shoppingCart'
  }),
  filters: {
  },
  methods: {
    ...mapActions({
      addToShoppingCart: 'addToShoppingCart',
      removeOneFromShoppingCart: 'removeOneFromShoppingCart'
    }),
    addProductToShoppingCart(product) {
      this.addToShoppingCart(product)
    },
    getProductAmount(productId) {
      if (!this.shoppingCart) {
        return 0
      } else {
        if (!this.shoppingCart[productId]) {
          return 0
        } else {
          return this.shoppingCart[productId].amount
        }
      }
    }
  },
  props: ['products']
}
</script>
<style scoped>
.products-holder {
  margin-top: 10px;
  width: 1200px;
  /* height: 800px; */
  margin-left: auto;
  margin-right: auto;
  /* background-color: #ffffff; */
}
.product-holder {
  float: left;
  width: 300px;
  height: 120px;
  background-color: #ffffff;
  padding: 10px;
  box-sizing: border-box;
  border: 1px solid #e4e7ed;
  text-align: left;
  margin-bottom: 10px;
}
.product-holder:hover {
  cursor: pointer;
  background-color: #f5f5f5;
}
.product-img {
  margin-top: 10px;
  width: 80px;
  height: 80px;
  float: left;
  /* background-color: blue; */
}
.product-desc {
  width: 198px;
  height: 100px;
  /* background-color: yellow; */
  float: left;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
}
.product-name {
  margin-left: 10px;
  font-size: 16px;
  font-weight: 600;
}
.product-options {
  margin-left: 10px;
  font-size: 15px;
  font-weight: 500;
  color: red;
}
.product-amount {
  margin-left: 10px;
  font-size: 13px;
  color: #606266;
}
.product-price {
  float: left;
  line-height: 26px;
}
.product-add-to-cart-button {
  float: right;
  margin-right: 10px;
  font-size: 14px;
  font-weight: normal;
  height: 26px;
  line-height: 26px;
  width: 100px;
  color: #000000;
  text-align: center;
  /* background-color: #409eff; */
  border-radius: 20px;
  border: 1px solid #ddd;
}
.product-add-to-cart-button-decrease {
  float: left;
  /* font-size: 12px; */
  width: 30px;
  height: 100%;
  background-color: #eee;
  border-top-left-radius: 20px;
  border-bottom-left-radius: 20px;
  border-right: 1px solid #ddd;
}
.product-add-to-cart-button-num {
  float: left;
  /* font-size: 12px; */
  width: 38px;
  height: 100%;
  background-color: #ffffff;
}
.product-add-to-cart-button-increase {
  float: right;
  /* font-size: 12px; */
  width: 30px;
  height: 100%;
  background-color: #eee;
  border-top-right-radius: 20px;
  border-bottom-right-radius: 20px;
  border-left: 1px solid #ddd;
}
</style>